package com.csun.dsms.service.impl;


import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import com.csun.dsms.comment.core.support.BaseService;
import com.csun.dsms.comment.core.utils.wrapper.WrapMapper;
import com.csun.dsms.comment.core.utils.wrapper.Wrapper;
import com.csun.dsms.mapper.HgMarketDataMapper;
import com.csun.dsms.model.huang.HgMarketData;
import com.csun.dsms.model.huang.HgQueryDto;
import com.csun.dsms.model.huang.HgYearMonthVo;
import com.csun.dsms.service.HgMarketDataService;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class HgMarketDataServiceImpl extends BaseService<HgMarketData>
        implements HgMarketDataService {

    @Resource
    private HgMarketDataMapper hgMarketDataMapper;
    
    @Override
    public List<HgMarketData> queryDataList(HgQueryDto hgQueryDto) {
        PageHelper.startPage(hgQueryDto.getPageNum(),hgQueryDto.getPageSize());
        return hgMarketDataMapper.queryListWithPage(hgQueryDto);
    }

    @Override
    public List<HgYearMonthVo> groupByMonth(HgQueryDto hgQueryDto) {
        return hgMarketDataMapper.groupByMonth(hgQueryDto);
    }

    @Override
    public Wrapper importMarketData(MultipartFile file) {
        try {
            ExcelReader reader = ExcelUtil.getReader(file.getInputStream());
            List<Map<String, Object>> ExcelList = reader.readAll();
            if (CollectionUtil.isEmpty(ExcelList)) {
                return WrapMapper.wrap(Wrapper.ERROR_CODE, "Excel不能为空！");
            }
            for (Map<String, Object> excelMap : ExcelList) {
                HgMarketData hgMarketData = new HgMarketData();
                hgMarketData.setStatisticalYear(Convert.toInt(excelMap.get("统计年份")));
                hgMarketData.setStatisticalMonth(Convert.toInt(excelMap.get("统计月")));
                hgMarketData.setArea(Convert.toStr(excelMap.get("数据的区域")));
                hgMarketData.setAreaLevel(Convert.toInt(excelMap.get("是市的还是区的")));
                hgMarketData.setIndustriesNumber(Convert.toInt(excelMap.get("行业编号")));
                hgMarketData.setConstructionEnterprisesNumber(Convert.toInt(excelMap.get("建筑业企业编号")));
                hgMarketData.setCommerceAndTradeNumber(Convert.toInt(excelMap.get("商贸编号")));
                hgMarketData.setRealEstateNumber(Convert.toInt(excelMap.get("房地产编号")));
                hgMarketData.setServiceIndustriesNumber(Convert.toInt(excelMap.get("服务行业编号")));
                hgMarketData.setTotalNumber(Convert.toInt(excelMap.get("总数")));
                hgMarketData.setEntryDepartment(Convert.toStr(excelMap.get("上传的部门")));
                hgMarketData.setCreator(Convert.toStr(excelMap.get("上传人")));
                hgMarketData.setApproved(Convert.toInt(excelMap.get("是否审核")));
                hgMarketData.setApproval(Convert.toStr(excelMap.get("审核人")));
                hgMarketData.setCreatedTime(new Date());
                hgMarketDataMapper.insert(hgMarketData);
            }
            return WrapMapper.wrap(Wrapper.SUCCESS_CODE, Wrapper.SUCCESS_MESSAGE, "导入数据成功！");
        } catch (Exception e) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, e.getMessage());
        }
    }
}
